package com.ones.sdp.kit.log;

import com.ones.sdp.kit.log.aspect.OnesLogAspect;
import com.ones.sdp.kit.log.event.OnesLogListener;
import com.ones.sdp.kit.log.service.OnesLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * ones-log配置
 *
 * @author Clark
 * @version 2025-05-04
 */
@Slf4j
@Lazy(true)
@EnableAsync
@AutoConfiguration
public class OnesLogAutoConfiguration {
    private static final int MAX_POOL_SIZE = 10;

    private static final int CORE_POOL_SIZE = 5;

    @Bean("asyncLogExecutor")
    public AsyncTaskExecutor asyncLogExecutor() {
        ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
        asyncTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
        asyncTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
        asyncTaskExecutor.setThreadNamePrefix("async-task-thread-pool-");
        asyncTaskExecutor.initialize();
        return asyncTaskExecutor;
    }

//    /**
//     * 日志Aop切面
//     *
//     * @return OnesLogService
//     */
//    @Bean
//    @ConditionalOnMissingBean(OnesLogService.class)
//    public OnesLogService onesLogService() {
//        log.warn("******************* 日志接口未实现，使用默认实现类 *******************");
//        return new OnesLogService.DefaultOnesLogServiceImpl();
//    }

    @Bean
    @ConditionalOnBean(OnesLogService.class)
    public OnesLogListener onesLogListener(OnesLogService onesLogService) {
        return new OnesLogListener(onesLogService);
    }

    @Bean
    @ConditionalOnBean(OnesLogListener.class)
    public OnesLogAspect onesLogAspect() {
        return new OnesLogAspect();
    }
}